/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Control.ControlOwner;
import Control.ControlPet;
import Entity.Owner;
import Services.OwnerService;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Nicolas
 */
public class PetCreatePanel extends javax.swing.JPanel {

    private HPVAS father;
    ControlPet cp;
    ControlOwner co;

    public PetCreatePanel(HPVAS father) {
        initComponents();
        this.father = father;
        this.setVisible(true);
        cp = new ControlPet();
        co = new ControlOwner();
        repaintOwnerTable();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        file_chooser = new javax.swing.JFileChooser();
        main_label = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        age_label = new javax.swing.JLabel();
        w_label = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        image_panel = new javax.swing.JPanel();
        create_button = new javax.swing.JButton();
        search_button = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        owner_table = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        cancel_button = new javax.swing.JButton();
        name_text = new javax.swing.JTextField();
        species_text = new javax.swing.JTextField();
        age_text = new javax.swing.JTextField();
        weight_text = new javax.swing.JTextField();
        image_text = new javax.swing.JTextField();

        setMaximumSize(new java.awt.Dimension(465, 284));
        setMinimumSize(new java.awt.Dimension(465, 284));
        setPreferredSize(new java.awt.Dimension(465, 284));

        main_label.setText("Pet");

        jLabel2.setText("Name");

        jLabel3.setText("Species");
        jLabel3.setMaximumSize(new java.awt.Dimension(27, 14));
        jLabel3.setMinimumSize(new java.awt.Dimension(27, 14));
        jLabel3.setPreferredSize(new java.awt.Dimension(27, 14));

        age_label.setText("Age");
        age_label.setMaximumSize(new java.awt.Dimension(27, 14));
        age_label.setMinimumSize(new java.awt.Dimension(27, 14));
        age_label.setPreferredSize(new java.awt.Dimension(27, 14));

        w_label.setText("Weight");
        w_label.setMaximumSize(new java.awt.Dimension(27, 14));
        w_label.setMinimumSize(new java.awt.Dimension(27, 14));
        w_label.setPreferredSize(new java.awt.Dimension(27, 14));

        jLabel6.setText("Image");

        javax.swing.GroupLayout image_panelLayout = new javax.swing.GroupLayout(image_panel);
        image_panel.setLayout(image_panelLayout);
        image_panelLayout.setHorizontalGroup(
            image_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 131, Short.MAX_VALUE)
        );
        image_panelLayout.setVerticalGroup(
            image_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        create_button.setText("Create");
        create_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                create_buttonActionPerformed(evt);
            }
        });

        search_button.setText("Browse");
        search_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                search_buttonActionPerformed(evt);
            }
        });

        jLabel7.setText("Select Owner");

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Owner Name", "ID"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        owner_table.setViewportView(table);
        table.getColumnModel().getColumn(0).setResizable(false);
        table.getColumnModel().getColumn(1).setResizable(false);

        cancel_button.setText("Cancel");
        cancel_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_buttonActionPerformed(evt);
            }
        });

        name_text.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                name_textActionPerformed(evt);
            }
        });

        age_text.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                age_textKeyTyped(evt);
            }
        });

        weight_text.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                weight_textKeyTyped(evt);
            }
        });

        image_text.setText("<URL>");
        image_text.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                image_textMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                image_textMouseEntered(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(image_panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(create_button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cancel_button, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(w_label, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(age_label, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(main_label, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(image_text, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(search_button))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(name_text, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(species_text, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(age_text, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(weight_text, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(owner_table, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(main_label, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(name_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(species_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(age_label, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(age_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(w_label, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(weight_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(search_button)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel6)
                                .addComponent(image_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(image_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 41, Short.MAX_VALUE)
                                .addComponent(create_button)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cancel_button))))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(owner_table, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addGap(11, 11, 11))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void repaintOwnerTable() {
        List<Owner> owners = co.readAllOwner();
        Iterator it = owners.iterator();
        int row = 0;
        while (it.hasNext()) {
            Owner ow = (Owner) it.next();
            if (row < table.getRowCount()) {
                table.setValueAt(ow.getName() + " " + ow.getLastName(), row, 0);
                table.setValueAt(ow.getId(), row, 1);
            } else {
                DefaultTableModel model = (DefaultTableModel) table.getModel();
                model.addRow(new Object[]{ow.getName() + " " + ow.getLastName()});
            }
            row++;
        }
    }

    private boolean validarDatos() {
        if (name_text.getText().trim().equals("") || species_text.getText().trim().equals("")||age_text.getText().trim().equals("")||weight_text.getText().trim().equals("")) {
            main_label.setText("Pet (hay espacios en blanco)");
            return false;
        }else{
            main_label.setText("Pet");
        }
        if(image_text.getText().trim().equals("<URL>")||image_text.getText().trim().equals("Debe seleccionar una imagen...")){
            image_text.setText("Debe seleccionar una imagen...");
            return false;
        }else{
            if(!image_text.getText().contains(".png")){
            image_text.setText("Debe seleccionar una imagen valida (.png)...");
            return false;
        }
        }
        if(table.getSelectedRow()<0){
            main_label.setText("Pet (Debe seleccionar un dueño)");
            return false;
        }else{
            main_label.setText("Pet");
        }
        return true;
    }

    private void create_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_create_buttonActionPerformed
        if(validarDatos()){
        cp.crearPet(name_text.getText().trim(), species_text.getText().trim(), Integer.parseInt(age_text.getText().trim()), Float.parseFloat(weight_text.getText().trim()), image_text.getText().trim(), co.readOwner((Long) table.getValueAt(table.getSelectedRow(), 1)));
        father.setCurrent_panel(new MainPanel());
        father.change_panel();
        }
    }//GEN-LAST:event_create_buttonActionPerformed

    private void search_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_search_buttonActionPerformed
        file_chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        file_chooser.setMultiSelectionEnabled(false);
        int showOpenDialog = file_chooser.showOpenDialog(this);
        image_text.setText(file_chooser.getSelectedFile().getPath());
    }//GEN-LAST:event_search_buttonActionPerformed

    private void cancel_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_buttonActionPerformed
        father.setCurrent_panel(new MainPanel());
        father.change_panel();
    }//GEN-LAST:event_cancel_buttonActionPerformed

    private void name_textActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_name_textActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_name_textActionPerformed

    private void image_textMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_image_textMouseEntered
    }//GEN-LAST:event_image_textMouseEntered

    private void image_textMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_image_textMouseClicked
        if (image_text.getText().contains("<URL>")) {
            image_text.setText("");
        }
    }//GEN-LAST:event_image_textMouseClicked

    private void age_textKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_age_textKeyTyped
        int k = (int) evt.getKeyChar();
        if (k >= 97 && k <= 122 || k >= 65 && k <= 90) {
            age_label.setText(age_label.getText()+" Solo números");
            evt.consume();            
        }else{
            age_label.setText("Age");
        }
    }//GEN-LAST:event_age_textKeyTyped

    private void weight_textKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_weight_textKeyTyped
        int k = (int) evt.getKeyChar();
        if (k >= 97 && k <= 122 || k >= 65 && k <= 90) {
            w_label.setText(w_label.getText()+" Solo números");
            evt.consume();            
        }else{
            w_label.setText("Weigth");
        }
    }//GEN-LAST:event_weight_textKeyTyped

    protected MaskFormatter createFormatter(String s) {
        MaskFormatter formatter = null;
        try {
            formatter = new MaskFormatter(s);
        } catch (java.text.ParseException exc) {
            System.err.println("formatter is bad: " + exc.getMessage());
            System.exit(-1);
        }
        return formatter;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel age_label;
    private javax.swing.JTextField age_text;
    private javax.swing.JButton cancel_button;
    private javax.swing.JButton create_button;
    private javax.swing.JFileChooser file_chooser;
    private javax.swing.JPanel image_panel;
    private javax.swing.JTextField image_text;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel main_label;
    private javax.swing.JTextField name_text;
    private javax.swing.JScrollPane owner_table;
    private javax.swing.JButton search_button;
    private javax.swing.JTextField species_text;
    private javax.swing.JTable table;
    private javax.swing.JLabel w_label;
    private javax.swing.JTextField weight_text;
    // End of variables declaration//GEN-END:variables
}
